#include "stdafx.h"

#include "MsgBody.h"

#pragma warning(disable: 4267)
//
//	Export the msgbody to TXT file
//	In:	pSMS	- Pointer to s_SMS_t which contains data
//		ostream - opened TXT file for output
//	Re:	None
void msgbody_export(s_SMS_t *pSMS, FILE *ostream) {
#ifdef PM_DEBUG
	printf ("Exporting MSG: No.%d TYPE%d DATE(%s), TEL(%s)\n",
			pSMS->dbg_SMS_serial,	pSMS->type_SMS,
			pSMS->tch_SMSdate,		pSMS->tch_SMStele);
	fprintf (ostream,"MSG %d: TYPE%d|%s|%s|%s\n",
			pSMS->dbg_SMS_serial,	pSMS->type_SMS,
			pSMS->tch_SMSdate,		pSMS->tch_SMStele,
			pSMS->tch_content);
#else
	fprintf (ostream,"TYPE%d|%s|%s|%s\n",
			pSMS->type_SMS,			pSMS->tch_SMSdate,
			pSMS->tch_SMStele,		pSMS->tch_content);
#endif
}

//
//	Reset the msgbody structure
//	In:	pSMS - Pointer to s_SMS_t to be reseted
//	Re:	None
void msgbody_reset(s_SMS_t *pSMS) {
	memset(pSMS,0,sizeof(s_SMS_t));
	msgbody_set_SMSdate(pSMS,NULL);
	msgbody_set_SMStele(pSMS,NULL);
	strcpy_s(pSMS->tch_content,MAX_CONTENT_SIZE,"EMPTY_MSG");
}
//
//	Set pSMSdate string [NOT_FINISHED]
//	In:	pSMS		- pointer to s_SMS_t
//	    pSMSdate	- pointer to SMSdate string
//	Re:	 0 - Input string cutted!
//		>0 - Number of BYTES copied.
int msgbody_set_SMSdate (s_SMS_t *pSMS, const char *pSMSdate) {
	const char	*str_default_date = "UNKNOWN_DATE";
	int		len = 0;
	errno_t	err;
	if (pSMSdate == NULL) {
		len = strlen(str_default_date);
		err = strcpy_s(pSMS->tch_SMSdate, MAX_STRING_SIZE, str_default_date);
	} else {
// NOT_IMPLEMENTED
	}
	if (err != 0) {	// Error number conversion.
		len = 0;
#ifdef PM_DEBUG
		printf("Error %d: msgbody_set_SMSdate\n", (int)err);
#endif
	}
	return len;
}
//
//	Set telephone number string
//	In:	pSMSdate	- pointer to MNL, see pattern below
//		MNL (1B) + MN (xB) + 0X40 + 0X40
//	Re:	0 - Input string cutted!
//		>0 - Number of BYTES copied.
int msgbody_set_SMStele (s_SMS_t *pSMS, const char *pSMStele) {
	const char	*str_default_tele = "UNKNOWN_TEL";
	int		len = 0;
	errno_t	err;
	if (pSMStele == NULL) {
		len = strlen(str_default_tele);
		err = strcpy_s(pSMS->tch_SMStele, MAX_STRING_SIZE, str_default_tele);
	} else {
		len = (int)*pSMStele;	// Read MNL
		if (len != 0) {
			err = strncpy_s(pSMS->tch_SMStele, MAX_STRING_SIZE,
					pSMStele + 1, len);
		} else {
			err = USER_ERR_POSITION;	// Error while len = 0
		}
	}
	if (err != 0) {
		len = 0;
#ifdef PM_DEBUG
		printf("Error %d: msgbody_set_SMStele.\n", (int)err);
#endif
	}
	return len;
}
//
//	Set content string
//	In:	pcontent	- pointer to UD, see pattern below
//		UDL (1B) + UD(xB) + 0X40 + 0X40
//	Re:	0 - Input string cutted!
//		>0 - Number of BYTES copied.
int msgbody_set_content (s_SMS_t *pSMS, const char *pcontent) {
	const char	*str_default_content = "EMPTY_MSG";
	int		len = 0;
	errno_t	err;
	if (pcontent == NULL) {
		len = strlen(str_default_content);
		err = strcpy_s(pSMS->tch_content, MAX_CONTENT_SIZE, str_default_content);
	} else {
		len = (int)*pcontent;	// Read UDL
		if (len != 0) {
			err = strncpy_s(pSMS->tch_content, MAX_CONTENT_SIZE,
					pcontent + 1, len);
		} else {
			err = USER_ERR_POSITION;	// Error while len = 0
		}
	}
	if (err != 0) {
		len = 0;
#ifdef PM_DEBUG
		printf("Error %d: msgbody_set_content.\n", (int)err);
#endif
	}
	return len;
}
